package com.xiguaji.tiktok.v1.tiktok.ranking.repository.impl;

import com.xiguaji.tiktok.utils.jdbc.JdbcTemplateKit;
import com.xiguaji.tiktok.utils.jdbc.PageResponse;
import com.xiguaji.tiktok.v1.tiktok.ranking.dto.RankingResponse;
import com.xiguaji.tiktok.v1.tiktok.ranking.dto.SearchData;
import com.xiguaji.tiktok.v1.tiktok.ranking.model.Ranking;
import com.xiguaji.tiktok.v1.tiktok.ranking.repository.RankingExtRepository;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class RankingExtRepositoryImpl implements RankingExtRepository {

    @Autowired
    JdbcTemplateKit jdbcTemplateKit;

    @Override
    public PageResponse<RankingResponse> getListForPage(SearchData searchData){
        StringBuffer sql = new StringBuffer("FROM ranking ");
        List<Object> params = new ArrayList<>();
        sql.append("WHERE ranking_type = ? ");
        params.add(searchData.getRankingType());
        sql.append("AND belong = ? ");
        params.add(searchData.getDate());
        sql.append("AND blogger_type = ? ");

        if(null == searchData.getBloggerTypeId()){
            searchData.setBloggerTypeId(0);

        }
        params.add(searchData.getBloggerTypeId());
        sql.append("ORDER BY ranking ");
        val page = jdbcTemplateKit.getPage(RankingResponse.class, searchData.getPage(), searchData.getPageSize(), "SELECT *,REPLACE(REPLACE (REPLACE (avatar, 'sign-sg', 'amd-va'), 'sign-va', 'amd-va'),'webp','jpeg') avatar ", sql.toString(), params.toArray());
        return page;
    }

    @Override
    public List<Ranking> getList(SearchData searchData,Integer count){
        StringBuffer sql = new StringBuffer("SELECT * FROM ranking ");
        List<Object> params = new ArrayList<>();
        sql.append("WHERE ranking_type = ? ");
        params.add(searchData.getRankingType());
        sql.append("AND belong = ? ");
        params.add(searchData.getDate());
        sql.append("AND blogger_type = ? ");

        if(null == searchData.getBloggerTypeId()){
            searchData.setBloggerTypeId(0);

        }
        params.add(searchData.getBloggerTypeId());
        sql.append("ORDER BY ranking ");

        if(null != count){
            sql.append("limit 0,?");
            params.add(count);
        }
        return jdbcTemplateKit.select(Ranking.class, sql.toString(), params.toArray());
    }
}
